package cn.com.headfree.file.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.file.dao.IFileDao;
import cn.com.headfree.vo.file.DealFileVo;

@Repository("fileDao")
public class FileDaoImpl extends BaseDaoImpl<DealFileVo> implements IFileDao {

	@Override
	public void addFile(DealFileVo f) {
		String sql = "insert into t_file (id,business_id,order_num,file_id,file_name,business_type_id,business_node,creater_id,creater_name,create_time,updater_id,updater_name,update_time) values ";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{business_id,jdbcType=VARCHAR},").append("#{order_num,jdbcType=INTEGER},")
				.append("#{file_id,jdbcType=VARCHAR},").append("#{file_name,jdbcType=VARCHAR},")
				.append("#{business_type_id,jdbcType=VARCHAR},").append("#{business_node,jdbcType=VARCHAR},").append("#{creater_id,jdbcType=VARCHAR},")
				.append("#{creater_name,jdbcType=VARCHAR},").append("#{create_time,jdbcType=DATE},")
				.append("#{updater_id,jdbcType=VARCHAR},").append("#{updater_name,jdbcType=VARCHAR},")
				.append("#{update_time,jdbcType=DATE})");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", IdGen.uuid());
		params.put("business_id", f.getBusinessId());
		params.put("order_num", f.getOrderNum());
		params.put("file_id", f.getFileId());
		params.put("file_name", f.getFileName());
		params.put("business_type_id", f.getBusinessTypeId());
		params.put("business_node", f.getBusinessNode());
		params.put("creater_id", f.getCreaterId());
		params.put("creater_name", f.getCreaterName());
		params.put("create_time", new Timestamp(new Date().getTime()));
		params.put("updater_id", f.getUpdaterId());
		params.put("updater_name", f.getUpdaterName());
		params.put("update_time", new Timestamp(new Date().getTime()));
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public void deleteFileByBusinessId(String orignalId) {
		String sql = "delete from t_file ";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" where business_id=#{business_id,jdbcType=VARCHAR}");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("business_id", orignalId);
		this.executeSql(sqlBuilder.toString(), params);		
	}

	@Override
	public List<DealFileVo> getFilesByBusinessId(String businessId) {
		StringBuilder sqlBuilder = new StringBuilder(
				"select business_id,file_id,file_name,business_node,business_type_id,order_num ");
		sqlBuilder.append(" from t_file where 1=1 ");
		ArrayList<Object> arrayList = new ArrayList<Object>();
		if (!StringUtil.isNullOrEmpty(businessId)) {
			sqlBuilder.append(" and business_id=?");
			arrayList.add(businessId);
		}
		sqlBuilder.append(" order by order_num ");
		return this.findAllBySQL(DealFileVo.class, sqlBuilder.toString(), null, false,arrayList.toArray());
	}

}
